/**
 * 角色列表界面，包括角色的增删改查功能
 * @author linzongxue
 * @create_date 2012-1-7
 */
define(function(){
	var RoleGridPanel = Ext.extend(Ext.grid.GridPanel, {
		border: false,
		stripeRows: true,
	    enableHdMenu: false,
	    autoExpandColumn: 3,
		initComponent: function(){
			this.tbar = this.createTbar();
			this.store = new Ext.data.JsonStore({
			    idProperty: 'id',
			    fields: ['name', 'memo'],
			    url: 'sys/role/list.do'
			});
			this.columns = [
				new Ext.grid.RowNumberer(),
			    this.sm = new Ext.grid.CheckboxSelectionModel(),
				{header: '名称', width: 150, dataIndex: 'name'},
				{header: '备注', width: 80, dataIndex: 'memo'}
			];
			RoleGridPanel.superclass.initComponent.call(this);
			this.on({
				afterrender: function(){
					this.refresh();
				},
				dblclick: function(){
					if(au.permit('sys.role.modify')) this.modifyRole();
				},
				scope: this
			});
			this.getSelectionModel().on({
				rowselect: function(sm, i, record){
					this.ownerCt.changeRole(record.json);
				},
				rowdeselect: function(sm, i, record){
					if (sm.getSelections().length == 0){
						this.ownerCt.changeRole(null);
					}
				},
				scope: this
			});
		},
		refresh: function(callback){
			var searchField = this.getTopToolbar().get('searchField');
			searchField.setValue(null);
			searchField.triggers[0].hide();
			this.getStore().load({callback:callback, scope: this});
		},
		addRole: function(){
			use('sys.role.RoleWindow', function(Win){
				new Win({
					callback: function(data){
						//添加成功后刷新列表并选中新行
						this.refresh(function(){
							var newRow = this.getStore().find('id', data.id);
							this.getSelectionModel().selectRow(newRow);
							this.getView().focusRow(newRow);
						});
					},
					cbScope: this
				}).show();
			}, this);
		},
		modifyRole: function(){
			var record = this.getSelectionModel().getSelected();
			if (!record){
				cu.hint('请选择要修改的角色');
				return;
			}
			use('sys.role.RoleWindow', function(Win){
				new Win({
					modifiedId: record.id,
					callback: function(data){
						for(var p in data){record.set(p, data[p]);}
						record.commit(); //去除单元格中的修改标记
					},
					cbScope: this
				}).show();
			}, this);
		},
		removeRoles: function(){
			var records = this.getSelectionModel().getSelections();
			if (records.length == 0){
				cu.hint('请选择要删除的角色');
				return;
			}
			var msg = '确定要删除选中的' + records.length + '个角色吗？';
			cu.confirm(msg, function(){
				cu.post({
					url: 'sys/role/delete.do',
					params: {ids: lu.pluck(records, 'json.id')},
					success: function(){
						this.getStore().remove(records);
					},
					scope: this
				});
			}, this);
		},
		/**
		 * 本地查找
		 */
		findRoles: function(key){
			this.getStore().each(function(record, index){
				var found = record.get('name').toLowerCase().indexOf(key.toLowerCase()) > -1;
				this.getView().getRow(index).style.display = found ? '' : 'none';
			}, this);
		},
		//不要用createToolbar，小心覆盖父类的方法
		createTbar: function(){
			return [
			    {text: '添加', handler: this.addRole, iconCls: 'icon-add',scope: this},
			    {text: '修改', handler: this.modifyRole, iconCls: 'icon-edit',scope: this},
			    {text: '删除', handler: this.removeRoles, iconCls: 'icon-delete',scope: this},
			    '->',
				new Ext.form.TwinTriggerField({
					id: 'searchField',
					trigger1Class: 'x-form-clear-trigger',
	                trigger2Class: 'x-form-search-trigger',
	                hideTrigger1: true,
	                emptyText: '输入角色名称查找',
	                onTrigger1Click: function(e){
	                	this.ownerCt.ownerCt.refresh();
	                },
	                onTrigger2Click: function(e){
	                	if (this.getValue() == ''){
	                		this.ownerCt.ownerCt.refresh();
	                	}
	                	else{
	                		this.ownerCt.ownerCt.findRoles(this.getValue());
	                		this.triggers[0].show();
	                	}
	                },
	                listeners: {
	                	specialKey: function(field, e){ //回车搜索
	                		if (e.getKey() == e.ENTER) this.onTrigger2Click();
	                	}
	                }
				}),
				'-'
			];
		}
	});
	return RoleGridPanel;
});